home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ww_tv.exe / TEDITOR2.CPP < prev    next >
C/C++ Source or Header  |  1992-08-24  |  18KB  |  761 lines

  1. /*------------------------------------------------------------*/
  2. /* filename - teditor2.cpp                                    */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*            TEditor member functions                        */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. //  Primatech Modification History:
  19. //
  20. //      11-06-91 JLS  Removed two apparently useless calls to setBufSize()
  21. //      11-08-91 JLS  Put two calls to setBufSize() back in
  22. //      11-12-91 JLS  Added code in insertBuffer() to support word wrapping
  23. //      11-19-91 JLS  Moved more code into wordWrapBuffer() and modified
  24. //                      insertBuffer() accordingly
  25. //      07-05-92 JLS  Synced with TV1.03 (BC3.10)
  26. //      08-24-92 JLS  Added code to write() and read() to handle new member data
  27. //      
  28.  
  29. #define Uses_TKeys
  30. #define Uses_TEditor
  31. #define Uses_TIndicator
  32. #define Uses_TEvent
  33. #define Uses_TScrollBar
  34. #define Uses_TFindDialogRec
  35. #define Uses_TReplaceDialogRec
  36. #define Uses_opstream
  37. #define Uses_ipstream
  38. #include <tv.h>
  39.  
  40. #if !defined( __STRING_H )
  41. #include <string.h>
  42. #endif  // __STRING_H
  43.  
  44. #if !defined( __CTYPE_H )
  45. #include <ctype.h>
  46. #endif  // __CTYPE_H
  47.  
  48. #if !defined( __DOS_H )
  49. #include <Dos.h>
  50. #endif  // __DOS_H
  51.  
  52. inline int isWordChar( int ch )
  53. {
  54.     return isalnum(ch) || ch == '_';
  55. }
  56.  
  57. #pragma warn -asc
  58.  
  59. int TEditor::countLines( void *buf, ushort count )
  60. {
  61. asm {
  62.     LES DI,buf
  63.     MOV CX,count
  64.     XOR DX,DX
  65.     MOV AL,0Dh
  66.     CLD
  67.     }
  68. __1:
  69. asm {
  70.     JCXZ __2
  71.     REPNE SCASB
  72.     JNE __2
  73.     INC DX
  74.     JMP __1
  75.     }
  76. __2:
  77.     return _DX;
  78. }
  79.  
  80. ushort scan( const void *block, ushort size, const char *str )
  81. {
  82.     ushort len = strlen( str );
  83. asm {
  84.     PUSH DS
  85.     LES DI,block
  86.     LDS SI,str
  87.     MOV CX,size
  88.     JCXZ __3
  89.     CLD
  90.     MOV AX,len
  91.     CMP AX,1
  92.     JB __5
  93.     JA __1
  94.     LODSB           // searching for a single character
  95.     REPNE SCASB
  96.     JNE __3
  97.     JMP __5
  98.     }
  99. __1:
  100. asm {
  101.     MOV BX,AX
  102.     DEC BX
  103.     MOV DX,CX
  104.     SUB DX,AX
  105.     JB  __3
  106.     LODSB
  107.     INC DX
  108.     INC DX
  109.     }
  110. __2:
  111. asm {
  112.     DEC DX
  113.     MOV CX,DX
  114.     REPNE SCASB
  115.     JNE __3
  116.     MOV DX,CX
  117.     MOV CX,BX
  118.     REP CMPSB
  119.     JE  __4
  120.     SUB CX,BX
  121.     ADD SI,CX
  122.     ADD DI,CX
  123.     INC DI
  124.     OR  DX,DX
  125.     JNE __2
  126.     }
  127. __3:
  128. asm {
  129.     XOR AX,AX
  130.     JMP __6
  131.     }
  132. __4:
  133. asm SUB DI,BX
  134. __5:
  135. asm {
  136.     MOV AX,DI
  137.     SUB AX,WORD PTR block
  138.     }
  139. __6:
  140. asm {
  141.     DEC AX
  142.     POP DS
  143.     }
  144.     return _AX;
  145. }
  146.  
  147. ushort iScan( const void *block, ushort size, const char *str )
  148. {
  149.     char s[256];
  150.     ushort len = strlen( str );
  151. asm {
  152.     PUSH DS
  153.     MOV AX,SS
  154.     MOV ES,AX
  155.     LEA DI,s
  156.     LDS SI,str
  157.     MOV AX,len;
  158.     MOV CX,AX
  159.     MOV BX,AX
  160.     JCXZ __9
  161.     }
  162. __1:
  163. asm {
  164.     LODSB
  165.     CMP AL,'a'
  166.     JB  __2
  167.     CMP AL,'z'
  168.     JA  __2
  169.     SUB AL,20h
  170.     }
  171. __2:
  172. asm {
  173.     STOSB
  174.     LOOP __1
  175.     SUB DI,BX
  176.     LDS SI,block
  177.     MOV CX,size
  178.     JCXZ __8
  179.     CLD
  180.     SUB CX,BX
  181.     JB  __8
  182.     INC CX
  183.     }
  184. __4:
  185. asm {
  186.     MOV AH,ES:[DI]
  187.     AND AH,0xDF
  188.     }
  189. __5:
  190. asm {
  191.     LODSB
  192.     AND AL,0xDF
  193.     CMP AL,AH
  194.     LOOPNE  __5
  195.     JNE __8
  196.     DEC SI
  197.     MOV DX,CX
  198.     MOV CX,BX
  199.     }
  200. __6:
  201. asm {
  202.     REPE CMPSB
  203.     JE  __10
  204.     MOV AL,DS:[SI-1]
  205.     CMP AL,'a'
  206.     JB  __7
  207.     CMP AL,'z'
  208.     JA  __7
  209.     SUB AL,20h
  210.     }
  211. __7:
  212. asm {
  213.     CMP AL,ES:[DI-1]
  214.     JE  __6
  215.     SUB CX,BX
  216.     ADD SI,CX
  217.     ADD DI,CX
  218.     INC SI
  219.     MOV CX,DX
  220.     JNE __4
  221.     }
  222. __8:
  223. asm {
  224.     XOR AX,AX
  225.     JMP __11
  226.     }
  227. __9:
  228. asm {
  229.     MOV AX, 1
  230.     JMP __11
  231.     }
  232. __10:
  233. asm {
  234.     SUB SI,BX
  235.     MOV AX,SI
  236.     SUB AX,wORD PTR block
  237.     INC AX
  238.     }
  239. __11:
  240. asm {
  241.     DEC AX
  242.     POP DS
  243.     }
  244.     return _AX;
  245. }
  246.  
  247. #pragma warn .asc
  248.  
  249. void TEditor::hideSelect()
  250. {
  251.     selecting = False;
  252.     setSelect(curPtr, curPtr, False);
  253. }
  254.  
  255. void TEditor::initBuffer()
  256. {
  257.     buffer = new char[bufSize];
  258. }
  259.  
  260.  
  261. Boolean TEditor::insertBuffer( char *p,
  262.                                ushort offset,
  263.                                ushort length,
  264.                                Boolean allowUndo,
  265.                                Boolean selectText,
  266.                                Boolean alwaysWrap
  267.                              )
  268. {
  269.     selecting = False;
  270.     ushort selLen = selEnd - selStart;
  271.     if( selLen == 0 && length == 0 && !alwaysWrap )
  272.         return True;
  273.  
  274.     ushort delLen = 0;
  275.     if( allowUndo == True )
  276.         if( curPtr == selStart )
  277.             delLen = selLen;
  278.         else
  279.             if( selLen > insCount )
  280.                 delLen = selLen - insCount;
  281.  
  282.     long newSize = long(bufLen + delCount - selLen + delLen) + length;
  283.  
  284.     if( newSize > bufLen + delCount )
  285.         if( newSize > 0xFFE0l || setBufSize(ushort(newSize)) == False )
  286.             {
  287.             editorDialog( edOutOfMemory );
  288.             return False;
  289.             }
  290.  
  291.     ushort selLines = countLines( &buffer[bufPtr(selStart)], selLen );
  292.  
  293.     if( curPtr == selEnd )
  294.         {
  295.         if( allowUndo == True )
  296.             {
  297.             if( delLen > 0 )
  298.                 memmove( 
  299.                          &buffer[curPtr + gapLen - delCount - delLen],
  300.                          &buffer[selStart],
  301.                          delLen
  302.                        );
  303.             insCount -= selLen - delLen;
  304.             }
  305.         curPtr = selStart;
  306.         curPos.y -= selLines;
  307.         }
  308.  
  309.     if( delta.y > curPos.y )
  310.         {
  311.         delta.y -= selLines;
  312.         if( delta.y < curPos.y )
  313.             delta.y = curPos.y;
  314.         }
  315.  
  316.     // Insert the text
  317.     if (length > 0)
  318.         memmove(buffer + curPtr, p + offset, length);
  319.  
  320.     // Change all internal pointers to be correct before calling word-wrap
  321.     short lines = countLines(buffer+curPtr, length);    // count no. of inserted lines
  322.     ushort prevline = prevLine(lineStart(curPtr));
  323.  
  324.     curPtr += length;
  325.     bufLen += length - selLen;
  326.     gapLen -= length - selLen;
  327.     limit.y += lines - selLines;
  328.     delta.y = max(0, min(delta.y, limit.y - size.y));
  329.  
  330.     if( allowUndo == True )
  331.         {
  332.         delCount += delLen;
  333.         insCount += length;
  334.         }
  335.  
  336.     // The wordWrapBuffer() function might change member data items (curPtr, etc.)
  337.     WordWrapState wrap_rc =
  338.         wordWrap ? wordWrapBuffer(prevline, curPtr-prevline+1) : wwsNoWrap;
  339.  
  340.     if (wrap_rc == wwsError)
  341.         return False;
  342.  
  343.     else if (wrap_rc == wwsNoWrap) {
  344.         // If the buffer wrapped, these fields were already adjusted
  345.         curPos.y += lines;
  346.         drawLine = curPos.y;
  347.         drawPtr = lineStart(curPtr);
  348.         curPos.x = charPos(drawPtr, curPtr);
  349.     }
  350.  
  351.     if (selLines == 0 && lines == 0)
  352.         update(ufLine);
  353.     else
  354.         update(ufView);
  355.  
  356.     if( selectText == False )
  357.         selStart = curPtr;
  358.     selEnd = curPtr;
  359.  
  360.     // Don't mark clipboard as modified -- it will ask to be saved if it is
  361.     if (!isClipboard())
  362.         modified = True;
  363.  
  364.     setBufSize(bufLen + delCount);      // Shrink buffer to minimum size
  365.  
  366.     return True;
  367. }
  368.  
  369.  
  370. Boolean TEditor::insertFrom( TEditor *editor )
  371. {
  372.     return insertBuffer( editor->buffer,
  373.                          editor->bufPtr(editor->selStart),
  374.                          editor->selEnd - editor->selStart,
  375.                          canUndo,
  376.                          isClipboard()
  377.                         );
  378. }
  379.  
  380.  
  381. ushort TEditor: